//
//  CurrentDeviceDetailViewController.m
//  HBSDKPro
//
//  Created by LRB_C on 2020/8/17.
//  Copyright © 2020 林瑞彬. All rights reserved.
//

#import "CurrentDeviceDetailViewController.h"

@interface CurrentDeviceDetailViewController ()
@property (weak, nonatomic) IBOutlet UITextView *resutlTF;

@end

@implementation CurrentDeviceDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 计步 心率 血压 睡眠 体温 等历史数据 读取前需打开
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation enableMassDataNotify:YES];
}

- (IBAction)test:(UIButton *)sender {
    [self.view endEditing:YES];
    [self readDeviceInfo];
}
- (IBAction)test2:(id)sender {
      [self.view endEditing:YES];
    _resutlTF.text = @"";
    [self getSetInfo];
}

//设置天气信息
-(void)setWeatherInfo{
    BleWeatherSetModel *weatherInfo = [BleWeatherSetModel new];
    weatherInfo.type =  5;
    weatherInfo.currentTemp = 11;
    weatherInfo.pm25 = 8;
    weatherInfo.minTemp = 5;
    weatherInfo.maxTemp = 20;
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation sendWeatherInfoCmdWithModel:weatherInfo];
    //发完看手环有没显示
      [self appenStrToTextView:[NSString stringWithFormat:@"设置天气数据 %@",[weatherInfo lrbDescription]]];
}


//心率血压历史数据
-(void)readHrmBphistoryData{
    [self appenStrToTextView:@"开始获取心率血压历史数据"];
    [[EGMBleHandler sharedBleHandler].operClient.hrmBpOperation writeCmdOfReadHrmAndBpHistoryDataWithBlock:^(NSArray<BleCacheHrmBpDataModel *> *models) {
        [self appenStrToTextView:@"获取心率血压历史数据结果:"];
        for ( BleCacheHrmBpDataModel *model in models) {
            [self appenStrToTextView:[model lrbDescription]];
            for (BleSingleHrmBpDataModel *smodel in model.hrmBpModelDataArray) {
                [self appenStrToTextView:[smodel lrbDescription]];
            }
        }
        
        
    }];
    
}


//获取最新小时步数数据
-(void)getHourStep{
     [self appenStrToTextView:@"开始获取记步小时 notify数据:"];
    [[EGMBleHandler sharedBleHandler].operClient.pedoOperation enableHourStepNotify:YES];
    [[EGMBleHandler sharedBleHandler].operClient.pedoOperation readHourStepData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleLatestHourStep:) name:NOTIFICATION_HOUR_STEP_UPDATED object:nil];
}

-(void)handleLatestHourStep:(NSNotification*)noti{
    
    dispatch_async(dispatch_get_main_queue(), ^{
        NSDictionary *dict = noti.userInfo;
         if (dict) {
             
              [self appenStrToTextView:@"获的最新小时记步notify数据:"];
             NSString *str = @"";
             for (NSString *key in dict.allKeys) {
                 str = [NSString stringWithFormat:@"%@,%@:%@",str,key,[dict objectForKey:key]];
             }
             [self appenStrToTextView:str];
         }
    });
 
    
}

//获取记步睡眠历史数据
-(void)getStepAndSleepOffLineData{
     [self appenStrToTextView:@"开始获取记步睡眠数据:"];
//    NSLog(@"开启读取数据开关");
   
     NSLog(@"下发读取数据指令");
    [[EGMBleHandler sharedBleHandler].operClient.pedoOperation writeCmdOfReadStepAndSleepOfflineDataWithBLock:^(NSArray<BLeStepDataModel *> *stepDatas, NSArray<BleSleepDataModel *> *sleepDatas) {
        [self appenStrToTextView:@"获取记步睡眠数据:"];
        [self appenStrToTextView:@"记步数据:"];
        for (BLeStepDataModel *stepMOdel in stepDatas) {
            [self appenStrToTextView:[stepMOdel lrbDescription]];
            for (BleStepHourCountModel *countModel in stepMOdel.allHourStepCount) {
                [self appenStrToTextView:[countModel lrbDescription]];
            }
        }
        
        [self appenStrToTextView:@"睡眠数据:"];
        for (BleSleepDataModel *sleepMOdel in sleepDatas) {
                   [self appenStrToTextView:[sleepMOdel lrbDescription]];
                   for (BleStepHourCountModel *countModel in sleepMOdel.hourRollCountArr) {
                       [self appenStrToTextView:[countModel lrbDescription]];
                   }
               }
        [self handleSleeDatas:sleepDatas];
    }];
    
  
}

-(void)readDeviceInfo{
     [self appenStrToTextView:@"同步当天计步和设备信息数据:"];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation enableReadDeviceNotify:YES];
      [[EGMBleHandler sharedBleHandler].operClient.infoOperation readDeviceCurrenInfoData];
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleDeviceInfo:) name:NOTIFICATION_DEVICEINFO_DATA_UPDATED object:nil];
      
}

-(void)handleSleeDatas:(NSArray*)sleepDatas{
    
    NSString *dateFormat = @"yyyy-MM-dd";
    NSString *dateStr = @"2020-08-19";
    NSDateFormatter *formater = [[NSDateFormatter alloc] init];
    [formater setDateFormat:dateFormat];
    NSDate *date = [formater dateFromString:dateStr];
    
    [[EGMBleHandler sharedBleHandler].operClient.pedoOperation calculateSleepDataForOneDay:date withAllSleepModel:sleepDatas hourOffset:0 resultBlock:^(BleSleepViewModel *resultModel) {
         [self appenStrToTextView:[NSString stringWithFormat:@"获取日期为%@的睡眠数据:",dateStr]];
         
        [self appenStrToTextView:[resultModel lrbDescription]];
        [self appenStrToTextView:[resultModel.hourSleepRollCoutDict lrbDescription]];
    }];
}


-(void)handleDeviceInfo:(NSNotification*)noti{
    [self appenStrToTextView:@"设备信息更新:"];
    BLeDeviceInfoModel *info = [noti.userInfo objectForKey:KEY_NOTIFICATION_USER_INFO];
    [self appenStrToTextView:[info lrbDescription]];
}

//测试 获取设备设置信息
-(void)getDeviceInfo{
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readModelData];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation enableBatteryNotify:YES];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readBatteryPower];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readSerialNumber];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readManufacturerData];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readHardwareVersionData];
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readSoftwareVersionData];
}

//获取记步设置信息
-(void)getSetInfo{
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation enableBleSetInoNotify:YES];
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation readBleSetDataModel];
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation setReadPedoSetBlock:^(BlePedoSetModel *model) {
        NSString *resutStr = [NSString stringWithFormat:@"获取记步设置信息：%@",[model lrbDescription]];
        [self appenStrToTextView:resutStr];

    }];
    
    
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation setRemindSwitchBlock:^(BleRemindSwitchSetInfoModel *model) {

        NSString *resutStr = [NSString stringWithFormat:@"获取开关设置信息：%@",[model lrbDescription]];
        [self appenStrToTextView:resutStr];
    }];
    
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation setClockTempSetBlock:^(BleClockAndTempSetModel *model) {
        NSString *resutStr = [NSString stringWithFormat:@"获取闹钟设置信息：%@,闹钟1:%@,闹钟2:%@ 闹钟:%@",[model lrbDescription],[model.clock1 lrbDescription],[model.clock2 lrbDescription],[model.clock3 lrbDescription]];
        [self appenStrToTextView:resutStr];

    }];
    
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation setLongsitHrmbpSetBlock:^(BleLongSitAndHrmBpSetModel *model) {
      NSString *resutStr = [NSString stringWithFormat:@"获取久坐提醒设置信息：%@",[model lrbDescription]];
     [self appenStrToTextView:resutStr];

    }];
 
}
//测试 保存几步设置到蓝牙设备
-(void)saveSetInfoForDeviceWithModel1{
    BlePedoSetModel *model = [BlePedoSetModel new];
    model.targetValue += 1000;
    model.height = 168;
    model.gender = 1;
    model.age  = 28;
    model.hrmInterval = 13;
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation savePedoSetInfoWithModel:model];
}

//重新设置
-(void)saveSetInfoForDeviceWithModel:(BlePedoSetModel*)model{
    model.targetValue += 1000;
    model.height += 1;
    model.gender = model.gender ? 0:1;
    model.age += 1;
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation savePedoSetInfoWithModel:model];
}


//获取设备信息
-(void)getDeviceCurrentInfo{
    // 通过通知 NOTIFICATION_DEVICEINFO_DATA_UPDATED 传出
 //   格式为 @{KEY_NOTIFICATION_USER_INFO:BLeDeviceInfoModel}
    [[EGMBleHandler sharedBleHandler].operClient.infoOperation readDeviceCurrenInfoData];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector((hanldeDeviceInfo:)) name:NOTIFICATION_DEVICEINFO_DATA_UPDATED object:nil];
}

-(void)hanldeDeviceInfo:(NSNotification*)nitification{
    NSDictionary *dict = [nitification userInfo];
    BLeDeviceInfoModel *info = [dict objectForKey:KEY_NOTIFICATION_USER_INFO];
    [self appenStrToTextView:[NSString stringWithFormat:@"获取设备信息:%@",[info lrbDescription]]];
}

//开关通知相关设置
-(void)setReminderSwitchSetInfo{
    BleRemindSwitchSetInfoModel *info = [[BleRemindSwitchSetInfoModel alloc] init];
    info.weChat = 1;
    info.qq = 0;
    info.startHour = 10;
    info.startMin = 0;
    info.endHour = 22;
    info.endMin = 0;
    info.yandex = 1;
     NSLog(@"设置通知开关 开启qq和微信通知");
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation saveReminderSwitchSetInfoWithModel:info];
}

//闹钟 和体温设置
-(void)saveClockInfo{
    BleClockAndTempSetModel *clock = [[BleClockAndTempSetModel alloc] init];
    BleClockModel *clockModel = [[BleClockModel alloc] init];
    clockModel.hour = 0;
    clockModel.min = 40;
    clockModel.isOpen = YES;
    clockModel.repeatDayArr =[NSSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]] ;
    clock.clock1 = clockModel;
    clock.tempInterval = 12; //温度测量间隔
    
    BleClockModel *clockModel2 = [[BleClockModel alloc] init];
    clockModel2.hour = 10;
    clockModel2.min = 40;
    clockModel2.isOpen = NO;
    clockModel2.repeatDayArr =[NSSet setWithArray:@[@"1",@"2",@"4",@"5"]] ;
    clock.clock2 = clockModel2;

    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation saveCloakAndTempSetInfoWithModel:clock];
}

//久坐提醒 和 心率血压报警设置
-(void)saveLongSitInfo{
    BleLongSitAndHrmBpSetModel *longSit = [BleLongSitAndHrmBpSetModel new];
    longSit.longSitOpen = 1;
    longSit.reminRepeatDayArr = [NSSet setWithArray:@[@"1",@"2",@"3",@"4",@"5"]];
    
    longSit.amStartHour = 9;
    longSit.amEndHour =12;
    longSit.pmStartHour = 14;
    longSit.pmEndHour = 18;
    
    longSit.bpLowerReminOpen = YES;
    longSit.lowerdbp = 77;
    
    [[EGMBleHandler sharedBleHandler].operClient.setInfoOperation saveLongSitAndHrmBpReminSetInfoWithModel:longSit];
    
}


-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}

-(void)appenStrToTextView:(NSString*)str{
    if (!str) return;
    NSString *text = _resutlTF.text;
    if (!text) text = @"";
    text = [NSString stringWithFormat:@"%@\n%@",text,str];
    _resutlTF.text = text;
    
    [_resutlTF scrollRectToVisible:CGRectMake(0, _resutlTF.contentSize.height - 100, self.view.frame.size.width, self.view.frame.size.height) animated:YES];
}
@end
